<?php

namespace app\admin\controller;

use app\admin\model\AdminMenu;
class AdminMenuController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }
    public function read()
    {
        $AdminMenu = new AdminMenu();
        if(isset($_GET['where'])){
           $AdminMenu =  $AdminMenu->where($_GET['where']);
        }
        $data = $AdminMenu->order('sort')->select();
        if (isset($_GET['type']) && $_GET['type']=='tree'){
            $tree_list = $AdminMenu->tree();
            return json($tree_list);
        }
        return json(array('code' => 0, 'msg' => "", 'data' => $data, 'count' => 100));
    }
    public function  del(){
        $result =AdminMenu::get(input('post.'))->delete();
        if($result){
            return json(['code'=>0,'msg'=>'删除成功']);
        }else{
            return json(['code'=>500,'msg'=>'删除失败']);
        }
    }
    public function add()
{
    $column= (new AdminMenu());
    $post_data = input('post.');
    if (input('post.type') == 3){
        $m = (new AdminMenu())->find(['menu_parent_id'=>input('post.menu_parent_id')]);
        if ($m['type']== 2){
            $post_data['menu_parent_id'] = $m['menu_id'];
            $post_data['control'] = $m['control'];
        }else{
            return json(['code' => 404, 'msg' => '只有菜单才能添加按钮']);
        }
    }
    $column->save($post_data);
    if ($column) {

        return json(['code' => 0, 'msg' => '操作成功']);
    } else {
        return json(['code' => 500, 'msg' => '操作失败']);
    }
}
    public function upd()
    {
        $column= (new AdminMenu());
        $post_data = input('post.');
        if (input('post.type') == 3){
            $m = (new AdminMenu())->find(['menu_parent_id'=>input('post.menu_parent_id')]);
            if ($m['type']== 2){
                $post_data['menu_parent_id'] = $m['menu_id'];
                $post_data['control'] = $m['control'];
            }else{
                return json(['code' => 404, 'msg' => '只有菜单才能添加按钮']);
            }
        }
        $column->isUpdate(true)->save($post_data);
        if ($column) {
            return json(['code' => 0, 'msg' => '操作成功']);
        } else {
            return json(['code' => 500, 'msg' => '操作失败']);
        }
    }
}
